package redsoxfan.gamejam.base;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

/**
 * Wrapper for a desktop game. The wrapper supports full screen toggling. A JFrame is used for the window.
 * @author RedSoxFan
 * @since 20 Apr 14
 * @version 1.0
 */
public class DesktopWrapper implements GameWrapper {
	/**
	 * The default screen
	 */
	private static final GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
	/**
	 * The game window
	 */
	private JFrame frame;
	/**
	 * The game instance
	 */
	private Game game;
	/**
	 * Wrapper for a desktop game. The wrapper supports full screen toggling. A JFrame is used for the window.
	 * The basic game implementation is used.
	 */
	public DesktopWrapper() {
		this(new Game(){
			/**
			 * Authors of the game
			 */
			public String authors() {
				return "RedSoxFan";
			}
			/**
			 * Painting code
			 */
			public void draw(Graphics g) {
			}
			/**
			 * Icon to use for the window
			 */
			public Image icon() {
				return new BufferedImage(32, 32, BufferedImage.TYPE_INT_RGB);
			}
			/**
			 * Initialization code
			 */
			public void init() {
			}
			/**
			 * A short title for the game. This will be used for the distribution files. Make sure it does
			 * not include any illegal file characters!
			 * @return Short title
			 */
			public String shortTitle() {
				return "GameBase";
			}
			/**
			 * Code to execute each frame
			 */
			public void tick() {
			}
			/**
			 * Title of the game
			 */
			public String title() {
				return "Game Base";
			}
		});
	}
	/**
	 * Wrapper for a desktop game. The wrapper supports full screen toggling. A JFrame is used for the window.
	 * @param gameInstance - Game instance to place into the window
	 */
	public DesktopWrapper(Game gameInstance) {
		game = gameInstance;
		game.start(this);
		frame = new JFrame(game.title());
		frame.setIconImage(game.icon());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(new BorderLayout());
		frame.add(game, BorderLayout.CENTER);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		game.addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent e) {
				if (e.getKeyCode() == game.fullscreenKey())
					setFullscreen(!isFullscreen());
			}
		});
		game.requestFocus();
	}
	/**
	 * Returns whether the wrapper is an applet. For this wrapper, it is false.
	 * @return false
	 */
	public boolean isApplet() {
		return false;
	}
	/**
	 * Returns whether the wrapper is running on the desktop. For this wrapper, it is true.
	 * @return true
	 */
	public boolean isDesktop() {
		return true;
	}
	/**
	 * Returns whether the game is being displayed in full screen
	 * @return Full screen state
	 */
	public boolean isFullscreen() {
		return device.getFullScreenWindow() != null && device.getFullScreenWindow().equals(frame);
	}
	/**
	 * Set the full screen state
	 * @param fulls - Desired full screen state
	 */
	public void setFullscreen(boolean fulls) {
		if (fulls) {
			game.setPreferredSize(new Dimension(1, 1));
			frame.setVisible(false);
			frame.dispose();
			frame.setUndecorated(true);
			frame.setVisible(true);
			device.setFullScreenWindow(frame);
			game.requestFocus();
		} else {
			game.setPreferredSize(new Dimension(1, 1));
			frame.setVisible(false);
			frame.dispose();
			frame.setUndecorated(false);
			frame.setVisible(true);
			device.setFullScreenWindow(null);
			game.requestFocus();
		}
	}
}
